﻿using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using SlamCms.Common;
using SlamCms.Data;

namespace SlamCms.Sample
{
	class Program
	{
		static void Main(string[] args)
		{
			// clean up
			GC.Collect();
			GC.WaitForPendingFinalizers();
			GC.Collect();

			using (var connection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=KnowledgeCenter;Integrated Security=true"))
			{
				connection.Open();

				DataContextSettings.Default.DatabaseConnectionFactory = () => connection;

				var stopWatch = Stopwatch.StartNew();

				var dataContext = new DataContext();

				stopWatch.Stop();

				Console.WriteLine("Initialization... {0}".F(stopWatch.Elapsed));

				// warm up
				var ci2 = dataContext.CreateQuery(QueryBuilderOptions.AllZones)
				                        .Cache(QueryCacheBehavior.NoCache)
				                        .IncludeTags()
				                        .Get();

				for (var i = 1; i < 6; i++)
				{
					Console.WriteLine("TEST RUN {0} ----------------------------------------", i);
					stopWatch = Stopwatch.StartNew();

					var ci = dataContext.CreateQuery(QueryBuilderOptions.AllZones)
										.Cache(QueryCacheBehavior.NoCache)
										.IncludeTags()
										.Get();

					stopWatch.Stop();

					Console.WriteLine("Get content... {0} items {1}".F(ci.Count(), stopWatch.Elapsed));
					Console.WriteLine(ci.Statistics);
				}

				Console.WriteLine("Press any key to continue...");
				Console.ReadKey();
			}
		}
	}
}
